home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 019 / blackjack / debug-outcom.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  3KB  |  122 lines

  1. /* outcom.c       print outcome of hand(s)
  2.  */
  3.  
  4. #include "local.h"
  5. #include "bj.h"
  6. #include "hndmgr.h"
  7.  
  8. static CASH value = 0;
  9.  
  10. /* outcom      - print outcome of hand and compute results
  11.  */
  12. CASH outcom(bet, tophand, isinsur, isdbl)
  13.    CASH  bet;           /* amount of player's bet */
  14.    short tophand;       /* how many player hands */
  15.    bool  isinsur;       /* player took insurance? */
  16.    bool  isdbl;         /* is player DBLDN? */
  17. {
  18.    void prmsg();
  19.    short h;             /* which hand */
  20.  
  21.    value = 0;
  22.    if (isinsur && isbj(DEALER))
  23.       prmsg(1, 1, "Insurance wins\n", bet / (isdbl ? 4 : 2));
  24.    else if (isinsur)
  25.       prmsg(1, 1, "Insurance loses\n", -bet / (isdbl ? 4 : 2));
  26.    if (isbj(DEALER) && !isbj(1))
  27.       prmsg(1, 1, "Dealer BJ beats all but BJ", -bet / (isdbl ? 2 : 1));
  28.    else if (isbj(DEALER) && isbj(1))
  29.       prmsg(1, 1, "Both BJ: push", (CASH)0);
  30.    else if (isbj(1))
  31.       prmsg(1, 1, "Your BJ wins 3 for 2", (3 * bet) / 2);
  32.    else {
  33.       for (h = 1; h <= tophand; ++h) {
  34.          if (21 < score(h))
  35.             value -= bet;        /* "Bust" message already printed */
  36.          else if (score(DEALER) == score(h))
  37.             prmsg(h, tophand, "Push", (CASH)0);
  38.          else if (score(DEALER) < score(h) || 21 < score(DEALER))
  39.             prmsg(h, tophand, "Win", bet);
  40.          else
  41.             prmsg(h, tophand, "Lose", -bet);
  42.       }
  43.    }
  44.    return (value);
  45. }
  46.  
  47. /* prmsg    - prints appropriate message
  48.  */
  49. void prmsg(h, tophand, s, delta)
  50.    short h;       /* which hand */
  51.    short tophand; /* how many hands */
  52.    char s[];      /* message */
  53.    CASH delta;    /* change of value ( + | - ) */
  54. {
  55.    if (tophand == 2)
  56.       printf("On hand %d, ", h);
  57.    printf("%s\n", s);
  58.    value += delta;
  59. }
  60.  
  61. #ifdef TRYMAIN
  62. static short bj[2] = 0;    /* isbj, for each hand */
  63. static short sc[3] = 0;    /* hand scores, for testing */
  64. main()
  65. {
  66.    char  line[BUFSIZ];     /* line of test input */
  67.    short len;              /* returned value from input fn */
  68.    short ibet;             /* player's bet, as short int */
  69.    short ins;              /* isinsur? */
  70.    short toph;             /* tophand? */
  71.    short dbl;              /* isdbl? */
  72.    CASH  value;            /* return from outcom */
  73.  
  74.    FOREVER {
  75.       printf("%-8s %-8s %-8s %-8s %-8s %-8s %-8s %-8s %-8s\n",
  76.             "bet", "toph", "ins", "dbl",
  77.             "bj[0]", "bj[1]", "sc[0]", "sc[1]", "sc[2]");
  78.       len = echoln(line, BUFSIZ);
  79.       if (len == EOF)
  80.          break;
  81.       if (9 != sscanf(line, "%hd %hd %hd %hd %hd %hd %hd %hd %hd",
  82.             &ibet, &toph, &ins, &dbl,
  83.             &bj[0], &bj[1], &sc[0], &sc[1], &sc[2]))
  84.             error("outcom input error", "");
  85.       value = outcom((CASH)ibet, toph, ins, dbl);
  86.       printf("outcom) = ");
  87.       printf(CASHOUT, value);
  88.       printf("\n");
  89.    }
  90. }
  91.  
  92. /* score       - dummy version for testing
  93.  */
  94. short score(h)
  95.    short h;    /* which hand */
  96. {
  97.    return(sc[h]);
  98. }
  99.  
  100. /* isbj        - dummy version for testing
  101.  */
  102. bool isbj(h)
  103.    short h;    /* which hand */
  104. {
  105.    return (bj[h]);
  106. }
  107.  
  108. /* echoln      - get and echo an input line
  109.  */
  110. short echoln(line, size)
  111.    char  line[];
  112.    short size;
  113. {
  114.    short len;
  115.  
  116.    if ((len = getln(line, size)) != EOF)
  117.       printf("%s", line);
  118.    return(len);
  119. }
  120. #endif
  121.  
  122.